package com.collabscm.waf.solution.persistence;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.collabscm.waf.exception.ApplicationException;
import com.collabscm.waf.solution.logging.Logger;
import com.collabscm.waf.solution.logging.LoggerFactory;
import com.collabscm.waf.vo.ValueObject;

/**
 * 动态查询语句的分解结果
 *
 * @author Zeng Raoxing
 */
public class DynamicQueryBreakdown extends ValueObject {
	private static final long serialVersionUID = 1L;
	/**
	 * Select子句，包括：select …… from …
	 */
	private String selectClause;
	/**
	 * 过滤条件列表
	 */
	private List<WhereCondition> conditions;
	/**
	 * 原始的动态查询语句
	 */
	private String originalDynamicQuery;
	
	private static final Logger logger = LoggerFactory.getLogger(DynamicQueryBreakdown.class);
	private static final Map<WhereJoinType, WhereClauseBreakdown> whereClauseMap = new HashMap<WhereJoinType, WhereClauseBreakdown>();
	
	public static void registerWhereClauseBreakdown(WhereJoinType type, WhereClauseBreakdown breakdown) {
		whereClauseMap.put(type, breakdown);
	}
	
	/**
	 * 查找具体的解析器
	 * @param type
	 */
	public static WhereClauseBreakdown findWhereClauseBreakdown(WhereJoinType type){
		WhereClauseBreakdown breakdown = whereClauseMap.get(type);
		if(breakdown == null) {
			throw new ApplicationException("未找到" +type+ "对应的WhereClauseBreakdown对象");
		}
		return breakdown;
	}
	
	/**
	 * 解析dynamicQuery，其中：dynamicQuery都是类似于select * from Entity e where @e.attr1=:attr1 and @e.attr2=:attr2
	 * @param dynamicQuery
	 */
	public DynamicQueryBreakdown(String dynamicQuery) {
		this.breakdown(dynamicQuery, WhereJoinType.AND);
	}
	
	public DynamicQueryBreakdown(String dynamicQuery, WhereJoinType whereJoinType) {
		this.breakdown(dynamicQuery, whereJoinType);
	}
	
	public void breakdown(String dynamicQuery, WhereJoinType whereJoinType) {
		this.originalDynamicQuery = dynamicQuery;
		int wherePos = dynamicQuery.indexOf(DynamicQuery.where);
		if(wherePos > 0) {
			selectClause = dynamicQuery.substring(0, wherePos);//找到select子句
		} else {
			throw new ApplicationException("动态查询语句{}中没有找到where(注意：where必须小写，否则也无法找到)", dynamicQuery);
		}
		String whereClause = dynamicQuery.substring(wherePos + 5);//过滤掉where之后的所有where子句
		WhereClauseBreakdown breakdown = findWhereClauseBreakdown(whereJoinType);
		this.conditions = breakdown.breakdown(whereClause);
	}
	
	public String getSelectClause() {
		return selectClause;
	}

	public List<WhereCondition> getConditions() {
		return conditions;
	}
	
	/**
	 * 描述分解结果
	 */
	public String descBreakdown() {
		StringBuffer result = new StringBuffer("原始动态查询语句：");
		result.append(originalDynamicQuery).append("。经过解析后完整的内容为：");
		result.append(selectClause);
		if(conditions != null) {
			result.append(DynamicQuery.where);
			for(WhereCondition condition: conditions) {
				result.append(condition.getCondition()).append(condition.getContinueJoinString());
			}
		}
		return result.toString();
	}
}
